/*
This codefile contains the functionality of the "HardwareOut" 
component, specified in the design report ("tussenrapport.doc").

Author:			Daan
Version: 		1
Last modified:	09-06-11
*/

// handler functions for state changes on the pins
void HandleCallToFloor3 ()
{
	ElevatorLogic_GoTo(3);
}
void HandleCallToFloor2 ()
{
	ElevatorLogic_GoTo(2);
}
void HandleCallToFloor1 ()
{
	ElevatorLogic_GoTo(1);
}
void HandleCallToFloor0 ()
{
	ElevatorLogic_GoTo(0);
}
void HandleCabinAtFloor3 ()
{
	ElevatorLogic_At(3);
}
void HandleCabinBelowFloor3 ()
{
	ElevatorLogic_Below(3);
}
void HandleCabinAboveFloor2 ()
{
	ElevatorLogic_Above(2);
}
void HandleCabinAtFloor2 ()
{
	ElevatorLogic_At(2);
}
void HandleCabinBelowFloor2 ()
{
	ElevatorLogic_Below(2);
}
void HandleCabinAboveFloor1 ()
{
	ElevatorLogic_Above(1);
}
void HandleCabinAtFloor1 ()
{
	ElevatorLogic_At(1);
}
void HandleCabinBelowFloor1 ()
{
	ElevatorLogic_Below(1);
}
void HandleCabinAboveFloor0 ()
{
	ElevatorLogic_Above(0);
}
void HandleCabinAtFloor0 ()
{
	ElevatorLogic_At(0);
}
void HandleOverheating ()
{
	ElevatorLogic_CallEmergency();
}
void HandleTemeratureChange ()
{
	// ?
}
void HandleEmergencyCall ()
{
	ElevatorLogic_CallEmergency();
}


// initializing function for this component
void HardwareOut_Initialize ()
{
	
}

/*
This function is to be run as frequent as possible. It checks when sensors are giving off signals, and calls the according handler function.
*/
void HardwareOut_CheckOutput ()
{
	if (PDR2_P25 == ON && PDR2_P24 == ON)
	{
		// shh, secret. hold cabin buttons 0 and 1 to return to normal mode after an emergency
		ElevatorLogic_ResumeOperations();
		time(1500); // otherwise you trigger a call to the first two floors immediately.
	}
	if (PDR0_P00 == ON)
	{
		HandleCallToFloor0();
	}
	if (PDR0_P01 == ON)
	{
		HandleCallToFloor1();
	}
	if (PDR0_P02 == ON)
	{
		HandleCallToFloor2();
	}
	if (PDR0_P03 == ON)
	{
		HandleCallToFloor3();
	}
	if (PDR2_P25 == ON)
	{
		HandleCallToFloor0();
	}
	if (PDR2_P24 == ON)
	{
		HandleCallToFloor1();
	}
	if (PDR2_P21 == ON)
	{
		HandleCallToFloor2();
	}
	if (PDR2_P20 == ON)
	{
		HandleCallToFloor3();
	}
	if (PDR5_P57 == ON)
	{
		HandleCabinAtFloor3();
	}
	if (PDR5_P56 == ON)
	{
		HandleCabinBelowFloor3();
	}
	if (PDR5_P55 == ON)
	{
		HandleCabinAboveFloor2();
	}
	if (PDR5_P54 == ON)
	{
		HandleCabinAtFloor2();
	}
	if (PDR5_P53 == ON)
	{
		HandleCabinBelowFloor2();
	}
	if (PDR5_P52 == ON)
	{
		HandleCabinAboveFloor1();
	}
	if (PDR5_P51 == ON)
	{
		HandleCabinAtFloor1();
	}
	if (PDR5_P50 == ON)
	{
		HandleCabinBelowFloor1();
	}
	if (PDR0_P05 == ON)
	{
		HandleCabinAboveFloor0();
	}
	if (PDR0_P04 == ON)
	{
		HandleCabinAtFloor0();
	}
	if (PDR0_P06 == ON)
	{
		HandleOverheating();
	}
	if (PDR0_P07 == ON)
	{
		HandleTemeratureChange();
	}
	if (PDR1_P17 == ON)
	{
		HandleEmergencyCall();
	}
}